
<%@ page import="news.example.Topic" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="news.example.News" %>
<%@ page import="news.service.*" %>
<%@ page import="news.util.PageBean" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../common/mytaglibs.jsp"%>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<style>
    .tool{
        height: 600px;
    }

    .news{
        margin-top: 20px;
    }
    .foot{
        font-size: 18px;
    }
    .foot a{
        margin-top: 10px;
    }


</style>
<div class="tool">
        <div class="news">
            <form method="post" action="queryPageNews">
                <table class="table">
                    <caption>新闻列表</caption>
                    <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>删除</th>
                    </tr>
                    </thead>
                    <tbody id="news_body">
                    </tbody>
                </table>
                <div class="foot">
                    当前页:<span id="currPageNo"></span>
                    每页:<span id="pageSize"></span>
                    总页:<span id="totalPageCount"></span>
                    <br>
                    <div id="changePage"></div>
                </div>
            </form>
        </div>
    </div>

<script>
    $(document).ready(function (){
        $.ajax({
            type: "POST",
            url: "${ctx}/queryPageNews?oper=service",
            dataType: "json",
            data: {
                "pageIndex":<%=request.getParameter("pageIndex")%>
            },
            success: function(data){
                let objectList = data.objectList;
                let newsTitle = "";
                let newsAuthor = "";
                let newsDel ="";
                let newsRes = "";

                    $.each(objectList,function (i,item){
                        // newsTitle += item.title;
                        // newsAuthor += '作者:'+item.username;
                        // newsDel += '<a href="/news/newslist/delnews.jsp?news_id='+item.id+'">删除</a><br>';
                        newsRes +='<tr>'
                        newsRes +=    '<td>'+item.title+'</td>';
                        newsRes +=    '<td>'+item.username+'</td>'
                        newsRes +=   '<th><a href="/news/newslist/delnews.jsp?news_id='+item.id+'">删除</a></th>';
                        newsRes +='</tr>';
                    });
                // $("#new_list_title").html(newsTitle);
                // $("#new_list_author").html(newsAuthor);
                // $("#new_list_del").html(newsDel);
                $("#news_body").html(newsRes);

                $("#currPageNo").html(data.currPageNo);
                $("#pageSize").html(data.pageSize);
                $("#totalPageCount").html(data.totalPageCount);

                let changePage = "";
                let totalPageCount = Number(data.totalPageCount);

                for (let i = 0; i <= totalPageCount; i++) {
                    if (i==1){
                        changePage += '<a href="<%=request.getContextPath()%>/news/main/mainnews-query.jsp?pageIndex=1">首页</a>';
                    }

                    if (i> 1 && i<totalPageCount)
                    changePage += '<a href="<%=request.getContextPath()%>/news/main/mainnews-query.jsp?pageIndex='+i+'">'+i+'</a>';
                    if (i==totalPageCount){
                        changePage += '<a href="<%=request.getContextPath()%>/news/main/mainnews-query.jsp?pageIndex='+totalPageCount+'">尾页</a>';
                    }
                }
                $("#changePage").html(changePage);
            }
        });
    })
</script>
